package com.liuyfly.leetcode.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.apache.commons.collections.CollectionUtils;

import com.liuyfly.leetcode.model.ListNode;
import com.liuyfly.leetcode.model.TreeNode;

import lombok.extern.slf4j.Slf4j;

/**
 * @author liuyangfang 2021/9/27 上午10:53
 */
@Slf4j
public class LeetcodeUtil {
    public static Integer[] generateArray(int length, int max) {
        Integer[] array = new Integer[length];
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            array[i] = Math.abs(random.nextInt()) % max;
        }
        return array;
    }

    public static <T> void printArray(T[] array) {
        if (array == null || array.length == 0) {
            log.info("array is null or empty");
            return;
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            builder.append(array[i].toString());
            if (i < array.length - 1) {
                builder.append(",");
            }
        }
        log.info("{}", builder);
        log.info("############################################################");
        System.out.println("\n\n");
    }

    public static void printArray(int[] array) {
        if (array == null || array.length == 0) {
            log.info("array is null or empty");
            return;
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            builder.append(array[i]);
            if (i < array.length - 1) {
                builder.append(",");
            }
        }
        log.info("{}", builder);
        log.info("############################################################");
        System.out.println("");
    }

    public static void printArray(char[] array) {
        if (array == null || array.length == 0) {
            log.info("array is null or empty");
            return;
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            builder.append(array[i]);
            if (i < array.length - 1) {
                builder.append(",");
            }
        }
        log.info("{}", builder);
        log.info("############################################################");
        System.out.println("\n\n");
    }

    public static <T> void printList(List<T> list) {
        if (CollectionUtils.isEmpty(list)) {
            log.info("list is empty");
            return;
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            builder.append(list.get(i).toString());
            if (i < list.size() - 1) {
                builder.append(",");
            }
        }
        log.info("{}", builder);
        log.info("############################################################");
    }

    public static void printTwoDimensionalArray(int[][] array) {
        if (array == null || array.length == 0) {
            log.info("array is null or empty");
            return;
        }
        StringBuilder builder = new StringBuilder();
        builder.append("\n");
        for (int[] outerArray : array) {
            for (int inner : outerArray) {
                if (inner >= 10) {
                    builder.append(inner).append(" ");
                } else {
                    builder.append(" ").append(inner).append(" ");
                }
            }
            builder.append("\n");
        }
        log.info("{}", builder);
        log.info("############################################################");
    }

    public static ListNode initListNode(ListNode listNode, int count) {
        if (count <= 0) {
            return listNode;
        }
        ListNode newListNode = new ListNode();
        newListNode.next = listNode;
        Random random = new Random();
        int val = Math.abs(random.nextInt()) % 90 + 10;
        newListNode.val = val;
        return initListNode(newListNode, --count);
    }

    public static ListNode initSortedListNode(int count) {
        ListNode head = new ListNode();
        if (count <= 0) {
            return head;
        }
        Integer[] intArray = generateArray(count, 99);
        Arrays.sort(intArray);
        head.val = intArray[0];
        ListNode current = head;
        for (int i = 1; i < count; i++) {
            ListNode newListNode = new ListNode();
            newListNode.val = intArray[i];
            current.next = newListNode;
            current = newListNode;
        }
        return head;
    }

    public static void printListNode(ListNode listNode) {
        List<Integer> list = new ArrayList<>();
        while (listNode != null) {
            list.add(listNode.val);
            listNode = listNode.next;
        }
        log.info("listNode:{}", JsonUtil.toJson(list));
    }

    /**
     * @param flag 是否随机
     * @return TreeNode
     */
    public static TreeNode<Integer> initTreeNode(boolean flag) {
        int[] array = { 5, 4, 6, 1, 2, 7, 8 };
        if (flag) {
            int random = Math.abs(new Random().nextInt()) % 10;
            for (int i = 0; i < array.length; i++) {
                array[i] = array[i] + random;
            }
        }
        TreeNode<Integer> root = new TreeNode<>(array[0]);
        TreeNode<Integer> left1 = new TreeNode<>(array[1]);
        TreeNode<Integer> right1 = new TreeNode<>(array[2]);
        root.left = left1;
        root.right = right1;
        TreeNode<Integer> left21 = new TreeNode<>(array[3]);
        TreeNode<Integer> left22 = new TreeNode<>(array[4]);
        TreeNode<Integer> right21 = new TreeNode<>(array[5]);
        TreeNode<Integer> right22 = new TreeNode<>(array[6]);
        left1.left = left21;
        left1.right = left22;
        right1.left = right21;
        right1.right = right22;
        return root;
    }
}
